#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` [-s sample_name] [-p out_prefix] [-t] reads1.fq reads2.fq
exit 65
}

while getopts  ":s:p:t:" opts
do
        case  $opts  in
        s) sample_name=$OPTARG;;
		p) out_prefix=$OPTARG;;
		t) threads=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))



if [ $# -lt 2 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

if test -z $threads; then threads=2; fi

echo sample: $sample_name. threads: $threads.
echo;echo;echo bowtie2 alignment
bowtie2 \
-x $ref_genome \
-1 $1 \
-2 $2 \
-S $out_prefix.sam \
--local \
--rg-id $sample_name \
--rg SM:$sample_name \
--rg PU:$sample_name \
--rg LB:$sample_name \
--rg PL:ILLUMINA \
-p $threads


#lb library
#pl platform
#pu platform unit #unique id
#sm sample_name
echo samtools sam to bam
samtools view -@$threads -bS $out_prefix.sam -o$out_prefix.bam && rm $out_prefix.sam

echo samtools sort
samtools sort -@$threads -T$out_prefix.t -o$out_prefix.sort.bam $out_prefix.bam && rm $out_prefix.bam

echo samtools index
samtools index $out_prefix.sort.bam



